package com.liuqi.loggers.core;

import com.liuqi.loggers.api.Logger;

/**
 * 日志实现类
 *
 * @see com.liuqi.loggers.api.Logger
 *
 * @author liuqi
 * @date 2024/10/14 15:46
 **/
public class Logging implements Logger {

    /**
     * 日志记录器实例名称
     */
    private final String name;

    /**
     * Context
     */
    private final Context context;

    /**
     * 日志级别
     */
    private Level level;

    public Logging(String name, Context context) {
        this.name = name;
        this.context = context;
    }

    @Override
    public void trace(String message) {
        logger(Level.TRACE, message, null);
    }

    @Override
    public void debug(String message) {
        logger(Level.DEBUG, message, null);
    }

    @Override
    public void info(String message) {
        logger(Level.INFO, message, null);
    }

    @Override
    public void warn(String message) {
        logger(Level.WARN, message, null);
    }

    @Override
    public void error(String message) {
        logger(Level.ERROR, message, null);
    }

    @Override
    public void trace(String format, Object... arguments) {
        logger(Level.TRACE, format, null, arguments);
    }

    @Override
    public void trace(String message, Throwable throwable) {
        logger(Level.TRACE, message, throwable);
    }

    @Override
    public void debug(String format, Object... arguments) {
        logger(Level.DEBUG, format, null, arguments);
    }

    @Override
    public void debug(String message, Throwable throwable) {
        logger(Level.DEBUG, message, throwable);
    }

    @Override
    public void info(String format, Object... arguments) {
        logger(Level.INFO, format, null, arguments);
    }

    @Override
    public void info(String message, Throwable throwable) {
        logger(Level.INFO, message, throwable);
    }

    @Override
    public void warn(String format, Object... arguments) {
        logger(Level.WARN, format, null, arguments);
    }

    @Override
    public void warn(String message, Throwable throwable) {
        logger(Level.WARN, message, throwable);
    }

    @Override
    public void error(String format, Object... arguments) {
        logger(Level.ERROR, format, null, arguments);
    }

    @Override
    public void error(String message, Throwable throwable) {
        logger(Level.ERROR, message, throwable);
    }

    @Override
    public boolean isTraceEnabled() {
        return levelEnabled(Level.TRACE);
    }

    @Override
    public boolean isDebugEnabled() {
        return levelEnabled(Level.DEBUG);
    }

    @Override
    public boolean isInfoEnabled() {
        return levelEnabled(Level.INFO);
    }

    @Override
    public boolean isWarnEnabled() {
        return levelEnabled(Level.WARN);
    }

    @Override
    public boolean isErrorEnabled() {
        return levelEnabled(Level.ERROR);
    }

    public void setLevel(Level level) {
        this.level = level;
    }

    public void logger(LoggerEvent event) {
        if (levelEnabled(event.getLevel())) {
            context.append(event);
        }
    }

    private boolean levelEnabled(Level level) {
        return level.ordinal() >= this.level.ordinal();
    }

    private void logger(Level level, String message, Throwable e) {
        logger(level, message, e, null);
    }

    private void logger(Level level, String message, Throwable e, Object... arguments) {
        logger(new LoggerEvent(level, message, arguments, e, name));
    }

}
